/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.controller;

import com.seari.bean.DeviceList;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.pojo.QueryDeviceList;
import com.seari.service.DeviceListService;
import com.seari.utils.StringHelper;
import java.sql.Timestamp;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Rainbow
 */
@Controller
@RequestMapping("dl/")
public class DeviceListController
{
	@Autowired
	private DeviceListService dls;

	@RequestMapping(value = "getDeviceList")
	@ResponseBody
	public DataGridBean<DeviceList> getDeviceList(HttpServletRequest req)
	{
		DataGridBean<DeviceList> grid = new DataGridBean<DeviceList>();
		int pageNum = Integer.parseInt(req.getParameter("page"));
		int rows = Integer.parseInt(req.getParameter("rows"));
		QueryDeviceList qdl = new QueryDeviceList();
		String station = req.getParameter("station");
		String system = req.getParameter("system");
		String devID = req.getParameter("devID");
		String devName = req.getParameter("devName");
		String from = req.getParameter("from");
		String to = req.getParameter("to");
		// String line = req.getParameter("line");
		String line = (String) req.getSession().getAttribute("LineID");
		if (StringUtils.isNotBlank(from))
		{
			qdl.setFrom(Timestamp.valueOf(from));
		}
		if (StringUtils.isNotBlank(to))
		{
			qdl.setFrom(Timestamp.valueOf(to));
		}
		qdl.setLimit(rows);
		qdl.setStart(rows * (pageNum - 1));
		if (StringUtils.isNotBlank(devID))
		{
			String[] devIDs = devID.split(",");
			qdl.setDevID(CollectionUtils.arrayToList(devIDs));
		}
		qdl.setDevName(devName);
		qdl.setLine(line);
		qdl.setStation(station);
		if (StringUtils.isNotBlank(system))
		{
			String[] systems = system.split(",");
			qdl.setSystem(CollectionUtils.arrayToList(systems));
		}
		List<DeviceList> deviceLists = dls.getDeviceListByParams(qdl);
		grid.setRows(deviceLists);
		grid.setTotal(dls.getDeviceListCount(qdl));
		return grid;
	}

	@RequestMapping(value = "getDeviceListStationID")
	@ResponseBody
	public MessageBean getDeviceListStationID(HttpServletRequest req)
	{
		MessageBean messageBean = new MessageBean();
		QueryDeviceList qdl = new QueryDeviceList();
		String line = (String) req.getSession().getAttribute("LineID");
		qdl.setLine(line);
		List<String> stationIDs = dls.getDeviceListStationID(qdl);
		if (!CollectionUtils.isEmpty(stationIDs) && stationIDs.size() > 0)
		{
			messageBean.setType(StringHelper.MESSAGE_TYPE_SUCCESS);
			messageBean.setData(stationIDs);
		}
		return messageBean;
	}

	@RequestMapping(value = "getDeviceListSystemID")
	@ResponseBody
	public MessageBean getDeviceListSystemID(HttpServletRequest req)
	{
		MessageBean messageBean = new MessageBean();
		QueryDeviceList qdl = new QueryDeviceList();
		String line = (String) req.getSession().getAttribute("LineID");
		qdl.setLine(line);
		List<String> systemIDs = dls.getDeviceListSystemID(qdl);
		if (!CollectionUtils.isEmpty(systemIDs) && systemIDs.size() > 0)
		{
			messageBean.setType(StringHelper.MESSAGE_TYPE_SUCCESS);
			messageBean.setData(systemIDs);
		}
		return messageBean;
	}

	@RequestMapping(value = "getDeviceListDevID")
	@ResponseBody
	public MessageBean getDeviceListDevID(HttpServletRequest req)
	{
		MessageBean messageBean = new MessageBean();
		QueryDeviceList qdl = new QueryDeviceList();
		String line = (String) req.getSession().getAttribute("LineID");
		qdl.setLine(line);
		List<String> devIDs = dls.getDeviceListDevID(qdl);
		if (!CollectionUtils.isEmpty(devIDs) && devIDs.size() > 0)
		{
			messageBean.setType(StringHelper.MESSAGE_TYPE_SUCCESS);
			messageBean.setData(devIDs);
		}
		return messageBean;
	}

	@RequestMapping(value = "getDeviceListLineID")
	@ResponseBody
	public MessageBean getDeviceListLineID(HttpServletRequest req)
	{
		MessageBean messageBean = new MessageBean();
		QueryDeviceList qdl = new QueryDeviceList();
		String line = (String) req.getSession().getAttribute("LineID");
		qdl.setLine(line);
		List<String> LineIDs = dls.getDeviceListLineID(qdl);
		if (!CollectionUtils.isEmpty(LineIDs) && LineIDs.size() > 0)
		{
			messageBean.setType(StringHelper.MESSAGE_TYPE_SUCCESS);
			messageBean.setData(LineIDs);
		}
		return messageBean;
	}
}
